package com.app.utils.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 改变View 画面的工具
 * Created by xuxinghai on 2016/6/14.
 */
public class ViewFrameUtils {

    /**
     * 设置textView左边图片
     * @param context
     * @param textView
     * @param image
     */
    public static void setTextLeftImage(Context context,TextView textView,int image){
        Drawable img = context.getResources().getDrawable(image);
        // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
        if(img == null){
            return;
        }
        img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
        textView.setCompoundDrawables(img, null, null, null); //设置左图标
        textView.setCompoundDrawablePadding(ViewSizeUtils.dp2px(context,5.0f));
    }

    /**
     * 设置右边的图片
     * @param context
     * @param textView
     * @param image
     */
    public static void setTextRightImage(Context context,TextView textView,int image){
        Drawable img = context.getResources().getDrawable(image);
        // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
        if(img == null){
            return;
        }
        img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
        textView.setCompoundDrawables(null, null, img, null); //设置左图标
        textView.setCompoundDrawablePadding(ViewSizeUtils.dp2px(context,5.0f));
    }



    /**
     * 根据view位置弹出键盘
     * @param view
     */
    public static void showSoftwareByView(final View view) {
        if(view == null){
            return;
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager inputManager =

                        (InputMethodManager) view.getContext().getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                if(inputManager != null && inputManager.isActive()){
                    inputManager.showSoftInput(view, 0);
                }

            }
        }, 10);

    }


    /**
     * 关闭输入法
     * @param activity
     * @param v
     */
    public static void closeInputSoft(Activity activity,View v) {
        if(activity == null || v == null || activity.isFinishing())return;
        InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && imm.isActive()) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }





    public static void showInputSoft(final Activity activity, final EditText editView){
        if(activity == null || editView == null || activity.isFinishing()) return;
        editView.post(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null && imm.isActive()) {
                    imm.showSoftInputFromInputMethod(editView.getWindowToken(), 0);
                }
            }
        });

    }

}
